package keeto.com.vn.classified.task;

import java.io.IOException;

import keeto.com.vn.classified.core.ClassifiedCore;
import keeto.com.vn.classified.core.ClassifiedListener;
import keeto.com.vn.classified.network.IProtocol;
import keeto.com.vn.classified.request.GetAndroidProductTypeRequest;
import keeto.com.vn.classified.response.GetAndroidProductTypeResponse;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;


public class TaskGetAndroidProductType extends AbstractTask {
	
	int iPageLimit;
	int iPageNumber;
	String CategoryType;
	String CategoryId;
	String GuaranteeStoreID;
	String ItemId;
	boolean isStore;
	String NotInPCodes;

	public TaskGetAndroidProductType(ClassifiedListener listener, ClassifiedCore core,
			String CategoryId,String GuaranteeStoreID, String itemID, int pageNumber, int pageLimit,
			String categoryType, boolean isStore,String NotInPCodes) {
		super(listener, core);
		this.iPageLimit = pageLimit;
		this.iPageNumber = pageNumber;
		this.CategoryType = categoryType;
		this.CategoryId = CategoryId;
		this.GuaranteeStoreID = GuaranteeStoreID;
		this.ItemId = itemID;
		this.isStore = isStore;
		this.NotInPCodes = NotInPCodes;
	}

	@Override
	public void run() throws ClientProtocolException, JSONException, IOException {
		GetAndroidProductTypeRequest request = new GetAndroidProductTypeRequest();
		request.setPageLimit(iPageLimit);
		request.setPageNumber(iPageNumber);
		request.setCategoryType(CategoryType);
		request.setCategoryid(CategoryId);
		request.setGuaranteeStoreID(GuaranteeStoreID);
		request.setItemId(ItemId);
		request.setStore(isStore);
		request.setNotInPCodes(NotInPCodes);
		GetAndroidProductTypeResponse reponse = json.getAndroidProductType(request);
		
		for (ClassifiedListener listener : listeners){
			listener.finishGetAndroidProductType(reponse.getReturnCode(), reponse.getListProductType(), reponse.getPageCount());
		}
	}

	@Override
	public void timeoutCallback() {
		for (ClassifiedListener ilistener:core.copyAndReconstructListeners(listener)){
			ilistener.finishGetAndroidProductType(IProtocol.STATUS_CODE_CONNECTION_TIMEOUT_502, null, 0);
		}
	}

}
